import git
import subprocess

def CheckMessageToCommit(CommitMessageToCheck):
    print(CommitMessageToCheck)

    if CommitMessageToCheck != "":
        PublishCommit(CommitMessageToCheck)
        print("Commit message is not empty\nPublishing commit...")
    else:
        print("Commit message is empty")

def PublishCommit(CommitMessage):
    repo = git.Repo("../.git/")

    commit = repo.commit()

    if repo.is_dirty():
        commit_message = "[UPDT] " + CommitMessage

        repo.git.add('-A')
        print('All files are added to the commit')

        commit_message = "[UPDT] " + CommitMessage
        committed = repo.index.commit(commit_message)

        print('Publishing a commit')
        origin = repo.remote('origin')
        origin.push('main')
        print('Commit published')
    else:
        print('The repository is clean, no changes for the commit')


